package com.ywy.controller;


import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.SystemPropertyUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import com.ywy.dao.FileDao;
import com.ywy.entity.Cloth;
import com.ywy.entity.ClothOneType;
import com.ywy.entity.ClothSize;
import com.ywy.entity.Color;
import com.ywy.entity.ColorAndSize;
import com.ywy.service.ClothService;
import com.ywy.service.ClothSizeService;
import com.ywy.service.ColorService;

@RestController
public class ClothController {

	@Autowired
	private ClothService cloths;

	@PostMapping(value = "admin/index/addcloth")
	public String getColorInfo(HttpServletRequest request, HttpServletResponse response)
			throws UnsupportedEncodingException {
		List<String> list = new ArrayList<String>();
		String filename = FileDao.getPhotoNewName();
		ServletContext servletContext = null;
		servletContext = request.getSession().getServletContext();
		// 数据库中存储格式:/webTest/imgs/***.jpg
		// 第一步:获取页面上上传的图片资源
		List<FileItem> items = FileDao.getRequsetFileItems(request, servletContext);
		boolean isLoadToSQL = false;
		for (FileItem item : items) {
			if (!item.isFormField()) {
				// 判断后缀名是否是jpg
				if (FileDao.isGif(item)) {
					isLoadToSQL = FileDao.saveFile(item, filename);
					String path = request.getContextPath() + "/WebContent/images/" + filename;
					System.out.println(path);
					// request.setAttribute("path", path);
					// request.getRequestDispatcher("look.jsp").forward(request, response);
				} else {
					System.out.println("后缀格式有误，保存文件失败");
				}
			} else {
				String paramName = item.getFieldName();
				String paramValue = item.getString();

				// //乱码问题
				paramValue = new String(paramValue.getBytes("iso8859-1"), "utf-8");
				list.add(paramValue);
				System.out.println(paramName + " = " + paramValue);
			}
		}
		Cloth cloth = new Cloth();
		cloth.setCloth_id(Integer.parseInt(list.get(0)));
		cloth.setCloth_name(list.get(1));
		cloth.setPrice(Double.parseDouble(list.get(2)));
		cloth.setTotal_count(Integer.parseInt(list.get(3)));
		cloth.setCloth_url(filename);
		ClothOneType clothOneType = new ClothOneType();
		clothOneType.setType_oneid(Integer.parseInt(list.get(4)));
		cloth.setCloth_type(clothOneType);
		ColorAndSize cas = new ColorAndSize();
		cas.setCloth(cloth);
		Color color = new Color();
		color.setColor_id(Integer.parseInt(list.get(6)));
		cas.setColor(color);
		ClothSize size = new ClothSize();
		size.setSize_id(Integer.parseInt(list.get(5)));
		cas.setSize(size);
		cas.setSize_count(Integer.parseInt(list.get(7)));
		if (cloths.addcloth(cloth, cas)) {
			return "成功";
		}else {
			return "失败";
		}
		
	}

}
